import java.util.Arrays;

/**
 * Content: Comparable接口、
 * --------------------------------------------------
 * Java 接口之 Comparable、Comparator和Cloneable: https://juejin.cn/post/7172486227323519012
 */

public class java_1003_class_Comparable {
    public static void main(String[] args) {

        // Comparable接口
        System.out.println("--------------------Comparable接口--------------------");

        Student3[] students = new Student3[3];
        students[0] = new Student3("小明", 20, 68);
        students[1] = new Student3("小红", 21, 78);
        students[2] = new Student3("小李", 20, 99);

        // 只有 Student3 实现了 Comparable, 才能调用 Arrays.sort(students) 函数
        System.out.println(Arrays.toString(students));
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));

        // Comparator是一个专用的比较器，当这个对象不支持自比较或者自比较函数不能满足要求时，可写一个比较器来完成两个对象之间大小的比较。

    }
}

class Student3 implements Comparable<Student3> {

    private String name;
    private int age;
    private float score;

    public Student3(String name, int age, float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student3 o) {
        // >0    <0    =0
        return this.age - o.age;
    }
}
